<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 xsi exsl">
	<!--
		documentHeaderItems is a vertical bar-delimited list of CDA
		document header items to import to SDA3 AdditionalDocumentInfo.
	-->
	<xsl:param name="documentHeaderItems" select="''"/>
	
	<xsl:variable name="documentHeaderItemsList">
		<xsl:choose>
			<!--
				If an "all" indicator was passed in, then set $documentHeaderItemsList to
				all items from POCD_MT000040.ClinicalDocument except for realmCode, typeId,
				templateId, code, languageCode, component.
			-->
			<xsl:when test="translate($documentHeaderItems,'al','AL')='ALL' or $documentHeaderItems='*'">
				<xsl:value-of select="'|id|title|effectiveTime|confidentialityCode|setId|versionNumber|copyTime|recordTarget|author|dataEnterer|informant|custodian|informationRecipient|legalAuthenticator|authenticator|participant|inFulfillmentOf|documentationOf|relatedDocument|authorization|componentOf|'"/>
			</xsl:when>
			<xsl:when test="string-length($documentHeaderItems)">
				<xsl:value-of select="concat('|',$documentHeaderItems,'|')"/>
			</xsl:when>
		</xsl:choose>
	</xsl:variable>
	
	<xsl:variable name="currentDateTime" select="isc:evaluate('timestamp')"/>
	<xsl:variable name="documentDateTime" select="/hl7:ClinicalDocument/hl7:effectiveTime/@value"/>
	<xsl:variable name="documentActionCode" select="/hl7:ClinicalDocument/hl7:relatedDocument/@typeCode"/>
	<xsl:variable name="sdaActionCodesEnabled" select="exsl:node-set($generalImportConfiguration)/sdaActionCodes/enabled/text()"/>
	<xsl:variable name="sdaOverrideExternalId" select="exsl:node-set($generalImportConfiguration)/sdaActionCodes/overrideExternalId/text()"/>
	<xsl:variable name="blockImportCTDCodeFromText" select="exsl:node-set($generalImportConfiguration)/blockImportCTDCodeFromText/text()"/>
	
	<!-- HCC文档类型-->
	<xsl:variable name="hccDocType" select="/hl7:ClinicalDocument/hl7:code/@code"/>
	
	<!-- HCC文档序号-->
	<xsl:variable name="hccDocNo" select="/hl7:ClinicalDocument/hl7:id/@extension"/>
	
	
	<!-- For repOrgconcatIdRootAndNumericExt the value in ImportProfile.xsl must be '1' or missing altogether in order to enable concat. -->
	<xsl:variable name="repOrgconcatIdRootAndNumericExt">
		<xsl:choose>
			<xsl:when test="exsl:node-set($generalImportConfiguration)/representedOrganizationId/concatRootAndNumericExtension/text()='0'">0</xsl:when>
			<xsl:when test="exsl:node-set($generalImportConfiguration)/representedOrganizationId/concatRootAndNumericExtension/text()='1'">1</xsl:when>
			<xsl:when test="not(string-length(exsl:node-set($generalImportConfiguration)/representedOrganizationId/concatRootAndNumericExtension/text()))">1</xsl:when>
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<xsl:variable name="sectionRootPath" select="/hl7:ClinicalDocument/hl7:component/hl7:structuredBody/hl7:component/hl7:section"/>
	<xsl:variable name="nonXMLBodyRootPath" select="/hl7:ClinicalDocument/hl7:component/hl7:nonXMLBody"/>
	<xsl:variable name="defaultAuthorRootPath" select="/hl7:ClinicalDocument/hl7:author[not(hl7:assignedAuthor/hl7:assignedAuthoringDevice)]"/>
	<xsl:variable name="defaultAuthoringDeviceRootPath" select="/hl7:ClinicalDocument/hl7:author[hl7:assignedAuthor/hl7:assignedAuthoringDevice]"/>
	<xsl:variable name="defaultInformantRootPath" select="/hl7:ClinicalDocument/hl7:informant"/>
	
	<xsl:key name="narrativeKey" match="//*" use="@ID"/>
	
	<xsl:variable name="narrativeImportModeGeneral">
		<xsl:choose>
			<xsl:when test="string-length(exsl:node-set($generalImportConfiguration)/narrativeImportMode/text())">
				<xsl:value-of select="exsl:node-set($generalImportConfiguration)/narrativeImportMode/text()"/>
			</xsl:when>
			<xsl:otherwise>1</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<xsl:variable name="orderItemDefaultCode" select="exsl:node-set($resultsImportConfiguration)/orderItemDefaultCode/text()"/>
	<xsl:variable name="orderItemDefaultDescription" select="exsl:node-set($resultsImportConfiguration)/orderItemDefaultDescription/text()"/>
	
	<!-- Medications PharmacyStatus -->
	<xsl:variable name="dischargeMedicationsPharmacyStatus">
		<xsl:choose>
			<xsl:when test="string-length(exsl:node-set($dischargeMedicationsImportConfiguration)/pharmacyStatus/text())">
				<xsl:value-of select="exsl:node-set($dischargeMedicationsImportConfiguration)/pharmacyStatus/text()"/>
			</xsl:when>
			<xsl:otherwise>DISCHARGE</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="medicationsPharmacyStatus">
		<xsl:choose>
			<xsl:when test="string-length(exsl:node-set($medicationsImportConfiguration)/pharmacyStatus/text())">
				<xsl:value-of select="exsl:node-set($medicationsImportConfiguration)/pharmacyStatus/text()"/>
			</xsl:when>
			<xsl:otherwise>MEDICATIONS</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="medicationsAdministeredPharmacyStatus">
		<xsl:choose>
			<xsl:when test="string-length(exsl:node-set($medicationsAdministeredImportConfiguration)/pharmacyStatus/text())">
				<xsl:value-of select="exsl:node-set($medicationsAdministeredImportConfiguration)/pharmacyStatus/text()"/>
			</xsl:when>
			<xsl:otherwise>ADMINISTERED</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<!-- Global variable to hold mapping of CDA encounter codes to SDA EncounterType -->
	<xsl:variable name="encounterTypeMaps" select="exsl:node-set($encountersImportConfiguration)/encounterTypeMaps"/>
	
	<!-- Global variables to hold lower case string and upper case string -->
	<xsl:variable name="lowerCase" select="'abcdefghijklmnopqrstuvwxyz'"/>
	<xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
	
	<!-- HCC获取encompassingEncounter中Encounter的ID,
	从/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.12']/@extension (住院号)
	或者/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.11']/@extension(门诊号)获取 -->
	<xsl:variable name="encompassingEncounterID">
		<xsl:choose>
		    <!--  先获取住院号 -->
			<xsl:when test="(string-length(/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.12']/@extension)) and (/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.12']/@extension !='-')">
			<xsl:value-of select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.12']/@extension"/>
			</xsl:when>
			<!--  再获取门诊号 -->
			<xsl:when test="string-length(/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.11']/@extension)">
			<xsl:value-of select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.11']/@extension"/>
			</xsl:when>
			<!--  再获取卫生事件住院号, 注意"-"意味空值 -->
			<xsl:when test="(string-length(/hl7:ClinicalDocument/hl7:component/hl7:structuredBody/hl7:component/hl7:section[hl7:code/@displayName='卫生事件']/hl7:entry/hl7:observation[hl7:code/@code='DE01.00.014.00']/hl7:value)) and (/hl7:ClinicalDocument/hl7:component/hl7:structuredBody/hl7:component/hl7:section[hl7:code/@displayName='卫生事件']/hl7:entry/hl7:observation[hl7:code/@code='DE01.00.014.00']/hl7:value !='-')">
			<xsl:value-of select="/hl7:ClinicalDocument/hl7:component/hl7:structuredBody/hl7:component/hl7:section[hl7:code/@displayName='卫生事件']/hl7:entry/hl7:observation[hl7:code/@code='DE01.00.014.00']/hl7:value"/>
			</xsl:when>
			<!--  再获取卫生事件门诊号 -->
			<xsl:otherwise>
			<xsl:value-of select="/hl7:ClinicalDocument/hl7:component/hl7:structuredBody/hl7:component/hl7:section[hl7:code/@displayName='卫生事件']/hl7:entry/hl7:observation[hl7:code/@code='DE01.00.010.00']/hl7:value"/>
			</xsl:otherwise>
			<!--  最後获取健康档案标识号 --><!--<xsl:value-of select="/hl7:ClinicalDocument/hl7:recordTarget/hl7:patientRole/hl7:id[@root='2.16.156.10011.1.2']/@extension"/>-->
		</xsl:choose>
	</xsl:variable>
	
	
	<!--获得就诊开始时间，用于Observation、Procedure、Order等默认时间-->
	<xsl:variable name="encounterFromTime">
		<xsl:choose>
			<!-- Encounter in 卫生事件章节 -->
			<xsl:when test="$hccDocType='C0001'">
				<xsl:value-of select="isc:evaluate('xmltimestamp',$sectionRootPath[hl7:code/@displayName=$hcc_HealthcareEvent]/hl7:entry/hl7:organizer/hl7:component/hl7:observation[hl7:code/@code='DE06.00.092.00']/hl7:value)"/>
			</xsl:when>
			<!-- Encounter in providerOrganization with less or more details -->
			<xsl:when test="($hccDocType='C0002') or ($hccDocType='C0003') or ($hccDocType='C0004') or ($hccDocType='C0005') or ($hccDocType='C0024') or ($hccDocType='C0025')">
				<xsl:value-of select="isc:evaluate('xmltimestamp',/hl7:ClinicalDocument/hl7:author/hl7:time/@value)"/>
			</xsl:when>
			<!-- Encounter in encompassingEncounter -->	
			<xsl:otherwise>
				<xsl:choose>
				<xsl:when test="/hl7:ClinicalDocument/hl7:componentOf/hl7:encompassingEncounter/hl7:effectiveTime/hl7:low/@value">
					<xsl:value-of select="isc:evaluate('xmltimestamp', /hl7:ClinicalDocument/hl7:componentOf/hl7:encompassingEncounter/hl7:effectiveTime/hl7:low/@value)"/>
				</xsl:when>
				<xsl:when test="not(/hl7:ClinicalDocument/hl7:componentOf/hl7:encompassingEncounter/hl7:effectiveTime/hl7:low/@value) and not(/hl7:ClinicalDocument/hl7:componentOf/hl7:encompassingEncounter/hl7:effectiveTime/hl7:high/@value) and /hl7:ClinicalDocument/hl7:componentOf/hl7:encompassingEncounter/hl7:effectiveTime/@value">
					<xsl:value-of select="isc:evaluate('xmltimestamp', /hl7:ClinicalDocument/hl7:componentOf/hl7:encompassingEncounter/hl7:effectiveTime/@value)"/>
				</xsl:when>
				</xsl:choose>
			</xsl:otherwise>		
		</xsl:choose>
	</xsl:variable>
	
	<!-- encounterIDs is a vertical bar-delimited string of encounter ids. -->
	<xsl:variable name="encounterIDs">
		<xsl:apply-templates select="/hl7:ClinicalDocument" mode="getEncounterIDs">
			<xsl:with-param name="encompassingEncounterID" select="$encompassingEncounterID"/>
		</xsl:apply-templates>
	</xsl:variable>
	
	<!-- encounterCount is the number of encounters in this document, based on $encounterIDs. -->
	<xsl:variable name="encounterCount">
		<xsl:choose>
			<xsl:when test="(string-length(translate($encounterIDs,translate($encounterIDs,'|',''),''))-1)>0">
				<xsl:value-of select="string-length(translate($encounterIDs,translate($encounterIDs,'|',''),''))-1"/>
			</xsl:when>
			<xsl:otherwise>0</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<!-- Gather all of the Encounter IDs into a vertical bar-delimited string. -->
	<xsl:template match="*" mode="getEncounterIDs">
		<xsl:param name="encompassingEncounterID"/>
		
		<xsl:variable name="encountersSectionEncounterIDs">
			<xsl:apply-templates select="$sectionRootPath[hl7:templateId/@root=$ccda-EncountersSectionEntriesOptional or hl7:templateId/@root=$ccda-EncountersSectionEntriesRequired]/hl7:entry" mode="getEncounterSectionIDs"/>
		</xsl:variable>
		<xsl:if test="string-length($encompassingEncounterID)"><xsl:value-of select="concat('|',$encompassingEncounterID)"/></xsl:if>
		<xsl:if test="string-length($encountersSectionEncounterIDs)"><xsl:value-of select="$encountersSectionEncounterIDs"/></xsl:if>
		<xsl:if test="string-length($encountersSectionEncounterIDs) or string-length($encompassingEncounterID)">|</xsl:if>
	</xsl:template>
	
	<xsl:template match="*" mode="getEncounterSectionIDs">
		<!--
			encounterIDTemp is used as an intermediate so that encounterID
			can be set up such that "000nnn" does NOT match "nnn" when
			comparing encounter numbers.
		-->
		<xsl:variable name="encounterIDTemp">
			<xsl:choose>
				<xsl:when test="string-length(hl7:encounter/hl7:id/@extension)">
					<xsl:value-of select="hl7:encounter/hl7:id/@extension"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="hl7:encounter/hl7:id/@root"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:variable name="encounterID" select="string($encounterIDTemp)"/>
		
		<xsl:value-of select="concat('|',$encounterID)"/>
	</xsl:template>
	
	<!-- Global variable for PlanImportConfiguration effectiveTimeCenter -->
	<xsl:variable name="planImportEffectiveTimeCenter" select="exsl:node-set($planImportConfiguration)/effectiveTimeCenter/text()"/>
</xsl:stylesheet>
